﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BLL;
using UtilityHelper;
using Model;
using DBUtility;
using System.Data;
using IYAKASAPortal.PrintAll;
using CrystalDecisions.CrystalReports.Engine;
using System.Web.UI.WebControls;
using System.Drawing.Printing;
using System.Threading;
using System.Reflection;
using gfVehicleLibraryAJ;
using ConvertInfo;

namespace IYAKASAPortal.Tools
{
    public class UseTools
    {
        public void UploadZ1(string strJCLSH)
        {
            Write18C55(strJCLSH, "Z1");
            Write18C81_zbzl(strJCLSH);
            Write_18C58_zbzl(strJCLSH);
        }

        public void Write18C55(string strJCLSH, string strJYXM)
        {
            try
            {
                VEHICLE_DISPATCH_BLL bll = new VEHICLE_DISPATCH_BLL();
                VEHICLE_DISPATCH info = bll.GetModel(strJCLSH);


                Vehicle_18C55_Para m_18c55_para = new Vehicle_18C55_Para();
                Networking netWorking = Networking.Instance;

                m_18c55_para.jylsh = info.AJLSH;
                m_18c55_para.jyjgbh = netWorking.jyjgbh;
                m_18c55_para.jcxdh = info.JCXH.ToString();
                m_18c55_para.jycs = Convert.ToInt32(info.JCCS);
                m_18c55_para.hpzl = info.HPZLDH;
                m_18c55_para.hphm = info.HPHM;
                m_18c55_para.clsbdh = info.VIN;
                m_18c55_para.gwjysbbh = string.Empty;
                m_18c55_para.jyxm = strJYXM;
                m_18c55_para.kssj = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                AJ_ModelToXML modelToXml = new AJ_ModelToXML();
                string strXML = modelToXml.ModelToXml("18C55", m_18c55_para);

                if (strXML.Contains("</jylsh>"))
                {
                    strXML = strXML.Replace("</jylsh>", "</jylsh><jclsh>" + strJCLSH + "</jclsh>");
                }

                IYASAKANetService.VehicleForIYASAKAService_AJSoapClient client = new IYASAKANetService.VehicleForIYASAKAService_AJSoapClient();
                string strRetXML = client.writeObjectOut(netWorking.xtlb, netWorking.jkxlh, "18C55", strXML);
            }
            catch(Exception ex)
            {

            }
        }

        public void Write18C81_zbzl(string strJCLSH)
        {

            try
            {
                VEHICLE_DISPATCH_BLL bll = new VEHICLE_DISPATCH_BLL();
                VEHICLE_DISPATCH info = bll.GetModel(strJCLSH);

                #region 整车逻辑
                RESULT_BRAKE_BLL brakeBll = new RESULT_BRAKE_BLL();
                RESULT_BRAKE brakeEntity = brakeBll.GetModel(strJCLSH);

                DataTable dt = new DataTable();
                LOGIN_VEHICLE_INFO_BLL loginBll = new LOGIN_VEHICLE_INFO_BLL();
                loginBll.GetModelList(ref dt, string.Format("VIN = '{0}'", info.VIN));
                RESULT_KERBMASS_BLL kerbmassBll = new RESULT_KERBMASS_BLL();
                RESULT_KERBMASS kerbmassEntity = null;
                if (dt.Rows.Count > 0)
                {
                    kerbmassEntity = new RESULT_KERBMASS();
                    try
                    {
                        kerbmassEntity.JCLSH = strJCLSH;
                        kerbmassEntity.ZBZLJCCS = info.JCCS.ToString();
                        kerbmassEntity.SCZBZL = brakeEntity.ZCZZ;
                        kerbmassEntity.ZBZLXZXX = "±100kg";
                        kerbmassEntity.ZBZLXZSX = "±3%";
                        kerbmassEntity.ZBZLJDWC = Math.Abs(Convert.ToInt32(dt.Rows[0]["ZBZL"]) - Convert.ToInt32(brakeEntity.ZCZZ)).ToString();
                        kerbmassEntity.ZBZLXDWC = (100 * Math.Abs(Convert.ToInt32(dt.Rows[0]["ZBZL"]) - Convert.ToInt32(brakeEntity.ZCZZ)) / Convert.ToInt32(dt.Rows[0]["ZBZL"])).ToString();

                        bool succ = Convert.ToInt32(kerbmassEntity.ZBZLJDWC) <= 100 || Convert.ToInt32(kerbmassEntity.ZBZLXDWC) <= 3;
                        if (succ)
                        {
                            kerbmassEntity.ZBZL_PD = "1";
                        }
                        else
                        {
                            kerbmassEntity.ZBZL_PD = "2";
                        }
                        kerbmassBll.Insert(kerbmassEntity);
                    }
                    catch (Exception ex)
                    {

                    }
                }

                #endregion

                Vehicle_18C81_ZBZL_Para m_18c81_zbzl = new Vehicle_18C81_ZBZL_Para();

                Networking netWorking = Networking.Instance;

                m_18c81_zbzl.jylsh = info.AJLSH;
                m_18c81_zbzl.jyjgbh = netWorking.jyjgbh;
                m_18c81_zbzl.jcxdh = info.JCXH.ToString();
                m_18c81_zbzl.jyxm = "Z1";
                m_18c81_zbzl.jycs = Convert.ToInt32(info.JCCS);
                m_18c81_zbzl.zbzl = Convert.ToInt32(brakeEntity.ZCZZ);

                if (kerbmassEntity == null)
                {
                    m_18c81_zbzl.zbzlpd = "0";
                }
                else
                {
                    m_18c81_zbzl.zbzlpd = kerbmassEntity.ZBZL_PD;
                }

                AJ_ModelToXML modelToXml = new AJ_ModelToXML();
                string strXML = modelToXml.ModelToXml("18C81", m_18c81_zbzl);

                if (strXML.Contains("</jylsh>"))
                {
                    strXML = strXML.Replace("</jylsh>", "</jylsh><jclsh>" + strJCLSH + "</jclsh>");
                }

                IYASAKANetService.VehicleForIYASAKAService_AJSoapClient client = new IYASAKANetService.VehicleForIYASAKAService_AJSoapClient();
                string strRetXML = client.writeObjectOut(netWorking.xtlb, netWorking.jkxlh, "18C81", strXML);


            }
            catch(Exception ex)
            {

            }
        }

        public void Write_18C58_zbzl(string strJCLSH)
        {
            try
            {
                VEHICLE_DISPATCH_BLL bll = new VEHICLE_DISPATCH_BLL();
                VEHICLE_DISPATCH info = bll.GetModel(strJCLSH);

                Vehicle_18C58_Para m_18c58_para = new Vehicle_18C58_Para();
                Networking netWorking = Networking.Instance;
                m_18c58_para.jylsh = info.AJLSH;
                m_18c58_para.jyjgbh = netWorking.jyjgbh;
                m_18c58_para.jcxdh = info.JCXH.ToString();
                m_18c58_para.jycs = Convert.ToInt32(info.JCCS);
                m_18c58_para.jyxm = "Z1";
                m_18c58_para.hpzl = info.HPZLDH;
                m_18c58_para.hphm = info.HPHM;
                m_18c58_para.clsbdh = info.VIN;
                m_18c58_para.gwjysbbh = string.Empty;
                m_18c58_para.jssj = DateTime.Now;

                AJ_ModelToXML modelToXml = new AJ_ModelToXML();
                string strXML = modelToXml.ModelToXml("18C58", m_18c58_para);

                if (strXML.Contains("</jylsh>"))
                {
                    strXML = strXML.Replace("</jylsh>", "</jylsh><jclsh>" + strJCLSH + "</jclsh>");
                }

                IYASAKANetService.VehicleForIYASAKAService_AJSoapClient client = new IYASAKANetService.VehicleForIYASAKAService_AJSoapClient();
                string strRetXML = client.writeObjectOut(netWorking.xtlb, netWorking.jkxlh, "18C58", strXML);
            }
            catch(Exception ex)
            {

            }

        }


        /// <summary>
        /// 获取当前流水号之前的所有检测流水号
        /// </summary>
        /// <param name="JCLSH"></param>
        /// <returns></returns>
        public static List<string> GetAllJCLSHFromResult(string JCLSH)
        {
            List<string> list = new List<string>();
            try
            {
                RESULT_VEHICLE_INFO_BLL bll = new RESULT_VEHICLE_INFO_BLL();
                RESULT_VEHICLE_INFO info = bll.GetModel(JCLSH);
                string strJCLSH = info.JCLSH.Substring(0, JCLSH.Length - 3);
                string strSql = string.Format("SELECT JCLSH FROM RESULT_VEHICLE_INFO WHERE ID <= {0} AND LEFT(JCLSH,CHARINDEX('-',JCLSH) -1) = '{1}' ", info.ID, strJCLSH);
                DataTable dt = new DataTable();
                DbHelper.GetTable(strSql, ref dt);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        list.Add(dt.Rows[i]["JCLSH"].ToString());
                    }
                }

            }
            catch (Exception ex)
            {
                WriteLogMsg("", ex.Message, "SYSTEM", LogHelper.LogType.Normal, "SYSTEM");
            }
            return list;
        }

        public PropertyInfo[] GetPropertyInfos(Type type)
        {
            return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
        }

        /// <summary>
        /// 号牌号码转换
        /// </summary>
        /// <param name="strHPHM">号牌号码</param>
        /// <param name="strHPZLDH">号牌种类代号</param>
        /// <returns></returns>
        public static string ChangeHPHM(string strHPHM, string strHPZLDH)
        {
            switch (strHPZLDH)
            {
                case "24":
                case "23":
                    if (!strHPHM.Contains("警"))
                        strHPHM += "警";
                    break;
                case "04":
                case "10":
                    //if (!strHPHM.Contains("领"))
                    //    strHPHM += "领";
                    break;
                case "03":
                case "09":

                    //if (!strHPHM.Contains("使"))
                    //    strHPHM += "使";
                    break;
                default:
                    break;
            }


            return strHPHM;
        }

        /// <summary>
        /// 实体属性反射
        /// </summary>
        /// <typeparam name="S">赋值对象</typeparam>
        /// <typeparam name="T">被赋值对象</typeparam>
        /// <param name="s"></param>
        /// <param name="t"></param>
        public static void AutoMapping<S, T>(S s, T t)
        {
            UseTools tools = new UseTools();
            PropertyInfo[] pps = tools.GetPropertyInfos(s.GetType());
            Type target = t.GetType();

            foreach (var pp in pps)
            {
                PropertyInfo targetPP = target.GetProperty(pp.Name);
                object value = pp.GetValue(s, null);

                if (targetPP != null && value != null && !string.IsNullOrEmpty(value.ToString()))
                {
                    targetPP.SetValue(t, value, null);
                }
            }
        }

        public T FillEntity<T>(DataRow dr)
        {
            T t = Activator.CreateInstance<T>();

            return t;
        }


        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="JCLSH">检测流水线号</param>
        /// <param name="logMsg">日志消息</param>
        /// <param name="operatorName">操作员名称</param>
        /// <param name="type">日志类型</param>
        /// <param name="funName">方法名称</param>
        public static void WriteLogMsg(string JCLSH, string logMsg, string operatorName, LogHelper.LogType type, string funName)
        {
            try
            {
                #region 日志记录
                LogHelper logHelper = LogHelper.GetSingleLogHelper();
                string strCategory = LogHelper.LogType.Normal.ToString();
                LOG_INFO logInfo = new LOG_INFO();
                logInfo.Category = strCategory;
                logInfo.HostName = System.Net.Dns.GetHostName();
                logInfo.IPAddress = System.Net.Dns.GetHostAddresses(logInfo.HostName).GetValue(1).ToString();
                logInfo.LogTime = DateTime.Now;

                logInfo.JCLSH = JCLSH;
                logInfo.Log_Infor = logMsg;
                logInfo.OpName = operatorName;
                logInfo.Name = logHelper.GetLogName(type);
                logInfo.FunName = funName;
                logHelper.WriteLog(logInfo);
                #endregion
            }
            catch (Exception ex)
            {

            }
        }



        #region 打印
        /// <summary>
        /// 获取打印机名称
        /// </summary>
        /// <returns></returns>
        public static List<ListItem> PrinterNameList()
        {
            List<ListItem> list = new List<ListItem>();
            PrintDocument prtdoc = new PrintDocument();
            foreach (String str in PrinterSettings.InstalledPrinters)
            {
                ListItem item = new ListItem();
                item.Text = str;
                list.Add(item);
            }
            return list;
        }


        public static void PrintAllReport(string ResultId, string PrinterName)
        {

            try
            {

                Thread t1 = new Thread(() =>
                {
                    PrintReport("", ResultId, PrinterName);
                    PrintReport("", ResultId, PrinterName);
                    PrintReport("", ResultId, PrinterName);
                });

            }
            catch (Exception ex)
            {

            }

        }

        public static void PrintReport(string ReportPath, string ResultId, string PrinterName)
        {
            try
            {
                SetCRDataSource setData = new SetCRDataSource();
                ReportDocument doc = new ReportDocument();
                doc = setData.BindAJReportEx(ResultId, ReportPath, false);
                doc.PrintOptions.PrinterName = PrinterName;
                doc.PrintToPrinter(1, true, 1, 1);
                doc.Dispose();
            }
            catch (Exception ex)
            {

            }
        }
        #endregion
    }
}